package com.neuronrobotics.replicator.gui.preview.android;

import java.util.ArrayList;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.View;

import com.neuronrobotics.replicator.gui.preview.model.STLWorkspaceModel;
import com.neuronrobotics.replicator.gui.preview.view.STLWorkspaceViewAndroid;
import com.neuronrobotics.replicator.gui.preview.view.STLWorkspaceViewListener;
import com.neuronrobotics.replicator.gui.stl.STLObject;

public class STLPreviewViewAndroid extends GLSurfaceView implements
		STLWorkspaceViewAndroid {

	private STLWorkspaceModel theModel;
	private AndroidWorkspaceRenderer theRenderer;
	private float xPick, yPick;

	private STLPreviewAndroidController theController;

	private ArrayList<STLWorkspaceViewListener> theListeners;

	private STLPreviewViewAndroid(Context context) {
		super(context);

		// Create an OpenGL ES 2.0 context.
		setEGLContextClientVersion(2);

		AndroidWorkspaceRenderer rend = new AndroidWorkspaceRenderer(this);

		// Set the Renderer for drawing on the GLSurfaceView
		setRenderer(rend);
		theRenderer = rend;

		// initialize controller which instantiates various controller classes
		// such as touch controllers, camera controls, etc.
		//TODO this should probably be set up elsewhere as a null model
		//doesn't help here at all
		theController = new STLPreviewAndroidController(this, theModel);

		theListeners = new ArrayList<STLWorkspaceViewListener>();

		// Render the view only when there is a change in the drawing data
		setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
	}

	public STLPreviewViewAndroid(Context context, STLWorkspaceModel stlwm) {
		this(context);
		theModel = stlwm;
		theRenderer.setWorkspaceModel(theModel);
		theController.setModel(theModel);
		//theController.placeAllObjectsOnWorkspace();
	}

	public STLPreviewAndroidController getTheController() {
		return this.theController;
	}

	public void initializeView() {
		// currently just a placeholder
	}

	@Override
	public void setWorkspaceModel(STLWorkspaceModel stlwm) {
		theModel = stlwm;
		this.theRenderer.setWorkspaceModel(theModel);
		if(theModel!=null) theController.placeAllObjectsOnWorkspace();
		initializeView();
	}

	@Override
	public void setCamera(float[] position, float[] lookAt) {
		theRenderer.setCamera(position, lookAt);
		for (STLWorkspaceViewListener lis : theListeners) {
			lis.alertCameraMoved(position, lookAt, new float[] { 0, 1, 0 });
		}
		requestRender();
	}

	@Override
	public void getCameraData(float[] position, float[] lookAt) {
		theRenderer.getCameraData(position, lookAt);
	}

	@Override
	public STLObject getPick(PickingRay mostRecentPickRay) {
		return this.theRenderer.getPick(xPick, yPick,mostRecentPickRay);
	}

	@Override
	public void addViewListener(STLWorkspaceViewListener newListener) {
		theListeners.add(newListener);
	}

	@Override
	public void setPickLocation(float x, float y) {
		this.theRenderer.clearPick();
		xPick = x;
		yPick = y;
		requestRender();
	}

	@Override
	public View getView() {
		return this;
	}
	
	private boolean isLoading = false;
	
	public void setLoading(boolean b){
		if(isLoading&&!b){
			//TODO may not be the right place for this?
		} else if(!isLoading&&b){
			
		}
	}
	
	public void clearPick() {
		theRenderer.clearPick();
		requestRender();
	}

	
}
